﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace Utils.Common
{
   public  class TextBitmap
    {


        //定义一个方法
        /// <summary>
        /// 把文字转换才Bitmap
        /// </summary>
        /// <param name="text"></param>
        /// <param name="font"></param>
        /// <param name="rect">用于输出的矩形，文字在这个矩形内显示，为空时自动计算</param>
        /// <param name="fontcolor">字体颜色</param>
        /// <param name="backColor">背景颜色</param>
        /// <returns></returns>
        public static Bitmap TextToBitmap(string text, Font font, Color fontcolor, Color backColor, int offsetX,int offsetY)
        {
            Graphics g;
            Bitmap bmp;
            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
           
           
            bmp = new Bitmap(1, 1);
            g = Graphics.FromImage(bmp);
            //计算绘制文字所需的区域大小（根据宽度计算长度），重新创建矩形区域绘图
            SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);

            int width = (int)(sizef.Width + offsetX);
            int height = (int)(sizef.Height + offsetY);
            //int height = (int)(sizef.Height + 1);

            Rectangle rect = new Rectangle(offsetX, offsetY, width, height);
            bmp.Dispose();
            bmp = new Bitmap(width, height);
          
            g = Graphics.FromImage(bmp);

            //使用ClearType字体功能
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            g.FillRectangle(new SolidBrush(backColor), rect);
            g.DrawString(text, font, new SolidBrush(fontcolor), rect, format);
            return bmp;
        }


        /// <summary>
        /// 合并图片
        /// </summary>
        /// <param name="imgBack"></param>
        /// <param name="img"></param>
        /// <param name="xDeviation"></param>
        /// <param name="yDeviation"></param>
        /// <returns></returns>
        public static Bitmap ImageShiftY( params Image [] images)
        {

            int totalWidth = 0;
            int totalHeight = 0;

            foreach (var img in images)
            {
                totalWidth += img.Width;

                totalHeight = totalHeight > img.Height ? totalHeight : img.Height;
            }
            int n = totalHeight % 8;
            if (n > 0)
            {
                totalHeight += (8 - n);
            }

            Bitmap imgdst = new Bitmap(totalWidth, totalHeight);
            using (Graphics gr = Graphics.FromImage(imgdst))
            {
                Rectangle rect = new Rectangle(0, 0, imgdst.Width, imgdst.Height);
                gr.FillRectangle(new SolidBrush(Color.White), rect);
                int x = 0;
                int y = 0;
                foreach (var img in images)
                {
                    gr.DrawImage(img, new PointF(x,y));
                    x += img.Width;
                }
            }
            return imgdst;
        }
       
    }
}
